package cn.edu.nju.setting;

import java.util.ArrayList;
import java.util.List;

import cn.edu.nju.R;
import com.devsmart.android.HorizontalListView;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CreateRuleActivity extends Activity{
	protected SettingProvider settingProvider;
	protected TagProvider tagProvider;
	protected FilterRule rule;
	protected List<String> copyTags;
	protected EditText ruleNameEdit;
	protected ExpandableHeightGridView tagsLayout;
	protected TextView titleText;
	protected HorizontalListView tagCategoryListView;
	protected TagCategoryListAdapter tagCategoryListAdapter;
	protected ExpandableHeightGridView tagPanel;
	protected ModifiableTagAdapter tagAdapter;
	protected TagAdapter recommendTagAdapter;
	protected List<String> recommendTags;
	protected TextView selectedCategory;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_rule);
        settingProvider = SettingProvider.getInstance(this);
        tagProvider = TagProvider.getInstance(this);
        
        initRuleAndCopyTags();
        
        // init widget
        initTopLayout();
        initRuleNameEdit();
        initRuleTags();
        initRecommendTags();
	}
	
	// derived class should override this function
	protected void initRuleAndCopyTags(){
		rule = new FilterRule(this);
        copyTags = new ArrayList<String>();
	}
	
	protected void initTopLayout(){
		initCancelButton();
		initSaveButton();
		
		titleText = (TextView)findViewById(R.id.modify_rule_name_text);
		titleText.setText(R.string.create_rule);
	}
	
	private void initCancelButton(){
		Button cancelButton = (Button) findViewById(R.id.modify_rule_delete_button);
		cancelButton.setText(R.string.cancel);
		cancelButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
		        finish();
			}
		});		
	}
	
	protected void initSaveButton(){
		Button saveButton = (Button)findViewById(R.id.modify_rule_save_button);
		saveButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				if(copyTags.size() > 0){
					rule.setRuleName(ruleNameEdit.getText().toString());				
					rule.setTags(copyTags);
					
					settingProvider.addRule(rule);
					settingProvider.setHasRuleAdded(true);
					finish();
				}
				else{
					Toast.makeText(CreateRuleActivity.this, getString(R.string.tag_empty), Toast.LENGTH_SHORT).show();
				}
			}
		});		
	}
	
	protected void initRuleNameEdit(){
		ruleNameEdit = (EditText) findViewById(R.id.rule_name_edit);
		ruleNameEdit.setText(rule.getRuleName());
		ruleNameEdit.addTextChangedListener(new TextWatcher() {			
			public void onTextChanged(CharSequence s, int start, int before, int count) {	
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			public void afterTextChanged(Editable s) {
				titleText.setText(s.toString());
			}
		});
	}
	
	protected void initRuleTags(){
		tagAdapter = new ModifiableTagAdapter(this, copyTags);
		tagsLayout = (ExpandableHeightGridView) findViewById(R.id.modify_rule_tags_layout);
		tagsLayout.setAdapter(tagAdapter);
	}
	
	protected void initRecommendTags(){
		tagCategoryListView = (HorizontalListView) findViewById(R.id.tag_category_list);
		tagCategoryListAdapter = new TagCategoryListAdapter(this, tagCategoryListView);
		tagCategoryListView.setAdapter(tagCategoryListAdapter);
		tagPanel = (ExpandableHeightGridView) findViewById(R.id.tag_section);
		
		tagCategoryListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// cancel the highlight of the previous category and highlight current selected category
				if(selectedCategory == null){
					// trick it is the first category
					selectedCategory = (TextView)(tagCategoryListView.getChildAt(0).findViewById(R.id.category_text));
				}
				selectedCategory.setBackgroundResource(R.drawable.rounded_rectangle);
				selectedCategory = (TextView)view.findViewById(R.id.category_text);
				selectedCategory.setBackgroundResource(R.drawable.selected_rounded_rectangle);
				initTagSection(position);
				
				tagCategoryListAdapter.setCurrentSelected(position);
			}			
		});
		
		// default select first category
		tagCategoryListAdapter.setCurrentSelected(0);
		initTagSection(0);
	}
	
	// show tags in category whose index is position
	protected void initTagSection(int position){
		recommendTags = tagProvider.getTagsInCategory(position);
		recommendTagAdapter = new TagAdapter(this, recommendTags);
		tagPanel.setAdapter(recommendTagAdapter);
		
		tagPanel.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String tag = recommendTags.get(position);
				if(!copyTags.contains(tag)){
					copyTags.add(tag);
					tagAdapter.notifyDataSetChanged();
				}
			}
		});
	}
}
